class DocumentsController < ApplicationController
  # GET /documents
  # GET /documents.xml
  def index
    #@documents = Document.find(:all, :conditions => {:status => 1}, :order => "created_at DESC")
    @documents = Document.paginate :page => params[:page], :conditions => {:status => 1}, :order => "created_at DESC",  :per_page => 10
    
    #respond_to do |format|
     # format.html # index.html.erb
     # format.xml  { render :xml => @documents }
    # end
  end

  def showtopten
    @documents = Document.find(:all, :conditions => {:status => 1}, :order => "stdev DESC", :limit => 10)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @documents }
    end
  end
  
  # GET /documents/1
  # GET /documents/1.xml
  def show
    #displays annotations and side panel
    @document = Document.find(params[:id])
    @userid = current_user
    @annotations = Annotation.paginate :page => params[:page], :conditions => {:user_id => @userid, :document_id => @document}, :order => "created_at", :per_page => 5
    @annotations_all = Annotation.paginate :page => params[:page], :conditions => {:document_id => @document}, :order => "created_at", :per_page => 5
    @annotations_nopg = Annotation.find(:all, :conditions => {:document_id => @document}, :order => "created_at")
  end

  # GET /documents/new
  # GET /documents/new.xml
  def new
    @document = Document.new
    #@jobs = Jobs.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/1/edit
  def edit
    @document = Document.find(params[:id])
  end

  # POST /documents
  # POST /documents.xml
  def create
    @document = Document.new(params[:document])
    @document.owner_id = current_user

    
    respond_to do |format|
      if @document.save
        flash[:notice] = 'Document successfully uploaded, please give us time as our system prepares your document.'

        #@document.docfile_file_name = generate_random_string + ".pdf"
              #tempfile = docfilename.split('.')
      #arg0 = "#{docid} #{docfilename}"
      #tempname = tempfile[0]
        @files = Dir.glob("public/mydocuments/#{@document.id}/*")
        @filename = File.basename(@files[0])
        @document.docfile_file_name = @filename
        @document.status = 0
        @document.save


  
        format.html { redirect_to :action => "index"}
        format.xml  { render :xml => @document, :status => :created, :location => @document }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end



  # PUT /documents/1
  # PUT /documents/1.xml
  def update
    @document = Document.find(params[:id])

    respond_to do |format|
      if @document.update_attributes(params[:document])
        flash[:notice] = 'Document was successfully updated.'
        format.html { redirect_to(@document) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.xml
  def destroy
    @document = Document.find(params[:id])
    @document.destroy

    respond_to do |format|
      format.html { redirect_to(documents_url) }
      format.xml  { head :ok }
    end
  end

  def find_document
    @document = Document.find(params[:id])
  end

  def comment
    Document.find(params[:id]).comments.create(params[:comment])
    flash[:notice] = "Added your comment"
    redirect_to :action => "show", :id => params[:id]
  end

  def tag_cloud
    @tags = Document.tag_counts_on(:tags)
  end

  def self.tag_event_list(page, tag)
    options = Document.find_options_for_find_tagged_with(tag).merge :page => page, :per_page => 10, :order => 'date DESC'
    paginate(options)
  end

  def rate
    @document = Document.find(params[:id])
    @document.rate(params[:stars], current_user, params[:dimension])
    id = "ajaxful-rating-#{!params[:dimension].blank? ? "#{params[:dimension]}-" : ''}document-#{@document.id}"
    render :update do |page|
      page.replace_html id, ratings_for(@document, :wrap => false, :dimension => params[:dimension], :small_stars => params[:small_stars])
      page.visual_effect :highlight, id
    end
  end

  #adds annotation to user
    def annotate
    @annotation = Annotation.new(params[:annotation])
    @annotation.document_id = params[:id]
    @annotation.user_id = current_user
    @annotation.tokens_array = params[:tokens]
    @annotation.save
    @document = Document.find(params[:id])
    @annotations_nopg = Annotation.find(:all, :conditions => {:document_id => @document}, :order => "created_at")
    flash[:annotate_notice] = "Annotated Document"
    respond_to do |format|
      format.html {redirect_to @document}
      format.js
    end
    end

    #retrieves comments of the selected annotation (highlight word)
    def retrieve_annotation
      #:all, :conditions => ["id = ?", params[:id]] )
      
    @annotation_comment = Annotation.find(params[:id])

#   @annotations_comments_all = Annotation.find(:all, :conditions => [:document_id => @annotation_comment.document_id AND :id != params[:id]], :order => "created_at")
    @annotations_comments_all = Annotation.find(:all, :conditions => ["document_id = ? AND id not in (?)", @annotation_comment.document_id, @annotation_comment.id])


      render :layout => false
    end




end
